package org.cttic.visualization.visualization.util;

import cn.hutool.json.JSONUtil;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.cttic.visualization.visualization.common.*;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.search.SimpleQueryStringQueryParser;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-12 13:58
 */
@Component
public class esUtil {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private EmployeeRepository employeeRepository;

    final String start="2020-05-01 00:00:00";
    final String typeName="_doc";


    public void esQuery(){
//        SimpleQueryStringQueryParser.Settings settings = SimpleQueryStringQueryParser.Settings.settingsBuilder().put("cluster.name", "elasticsearch") // 设置集群名
//                .put("client.transport.ignore_cluster_name", true) // 忽略集群名字验证, 打开后集群名字不对也能连接上
//                .build();
//        TransportClient client = TransportClient.builder().settings(settings).build()
//                .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("101.10.32.1", 9300)));

//        AggregationBuilder aggregationBuilder = AggregationBuilders
//                .terms("uid_aggs").field("uid").size(10000)
//                .subAggregation(AggregationBuilders.topHits("uid_top"));
//        List<EsDataStrip> distinctByVehicleNo = employeeRepository.findDistinctByVehicleNo("皖LB0697");
//        List<EsDataStrip> distinctByTansTimeBetween = employeeRepository.findDistinctByTansTimeBetween("2020-05-01 00:00:00", "2020-05-03 00:00:00");
        String startTime="2020-05-01 00:00:00";
        for (int i=1;i<12;i++){
            String endTime=null;
            if(i==1){
                endTime ="2020-05-01 00:0"+i*5+":00";

            }else {
                endTime ="2020-05-01 00:"+i*5+":00";
            }

            List<mongoData> esDataStrips = employeeRepository.findByTansTimeIsBetween(startTime,endTime);
            System.out.println("*******查询时间为：start:"+startTime+"end:"+endTime+"*******");
            for (mongoData esDataStrip : esDataStrips) {

                String jsonStr = JSONUtil.toJsonStr(esDataStrip);
                System.out.println("********查询结果为："+jsonStr+"*********");

                FindIterable<Document> vehiclePlate = mongoTemplate.getDb().getCollection(commonConf.mongo)
                        .find(Filters.eq("vehiclePlate", esDataStrip.getVehiclePlate()));
                if(vehiclePlate==null){
                    mongoTemplate.getDb().getCollection(commonConf.mongo).insertOne(Document.parse(jsonStr));
                }

//                .findOneAndUpdate(Filters.eq("vehiclePlate", esDataStrip.getVehiclePlate()),)
//                List<itineraryQueryData.Trip> list = DataUtil.itineraryQueryDataUtil(documents, feeFlag);
            }

            startTime=endTime;
            System.out.println("第"+i+"次结束");
        }
    }
}
